<?php
/**
 * Created by PhpStorm.
 * User: Kevin
 * Date: 2018/8/28
 * Time: 15:15
 */
namespace app\admin\model;
use think\Db;
use think\Model;
class MemberModel extends Model{
    protected $name = 'u_user';

    /*
     * 用户数据
     */
    public function getMemeberData($map,$od, $Nowpage, $limits){
        return $this->where($map)
            ->field('user_id,user_username,user_image,user_phone,user_time,user_status')
            ->page($Nowpage,$limits)
            ->order($od)
            ->select();
    }

    /*
     * 用户数量
     */
    public function getAllCount($map){
        return $this->where($map)->count();
    }

    /*
     * 收货地址
     */
    public function getAddress($id){
        return Db::name('user_address a')
            ->where('add_userid',$id)
            ->field('add_id,add_userid,add_address,add_phone,add_username,add_sex')
            ->select();
    }

    /*
     * 删除用户
     */
    public function memberDel($id){
        $res = $this->where('user_id',$id)->delete();
        if($res){
            return ['code'=>200,'msg'=>'删除成功'];
        }else{
            return ['code'=>100,'msg'=>'删除失败'];
        }

    }

    public function memberState($id,$num){
        if($num == 2){
            $msg = '禁用';
        }else{
            $msg = '启用';
        }
        Db::startTrans();// 启动事务
        try{
            $this->where ('user_id' , $id)->setField (['user_status' => $num]);
            Db::commit();// 提交事务
        }catch( \Exception $e){
            Db::rollback ();//回滚事务
            return ['code' => 100, 'data' => '', 'msg' => $msg.'失败'];
        }
    }
}